window.onload=function ()
{
    var vue = new Vue({
        el:"#app",
        data:{
            specificationList:[],
            //js对象中的数组需要初始化
            specification:{specName:'',optionList:[]},
            pages:0,
            current:1,
            searchSpecification:{},
            ids:[],
            checked:false
        },
        methods:{
           findByPage:function (current)
           {
               this.ids=[];
               axios.get("/specification/findByPage?current="+current,{params:this.searchSpecification}).then(function (response)
               {
                   vue.specificationList=response.data.rows;
                   vue.pages=response.data.pages;
                   vue.current=current;
                   //console.log(vue.specificationList);
               })
           },
            //添加一个规格选项
            addTabRow:function ()
            {
                this.specification.optionList.push({});
            },
            //删除一个规格选项
            deleteTabRow:function (index)
            {
                //splice根据索引删除，第一个参数是索引，第二个参数是要删除的个数
                this.specification.optionList.splice(index,1);
            },
            saveOrUpdate:function (specification)
            {
                var url="save";
                if (this.specification.id)
                {
                    url="update";
                }
                axios.post("/specification/"+url,this.specification).then(function (response)
                {
                    if (response.data)
                    {
                        vue.findByPage(vue.current);
                    }
                    else
                    {
                        alert("请不要瞎搞");
                    }
                });
            },
            show:function (specification)
            {
                axios.get("/specification/findOptionList?id="+specification.id).then(function (response)
                {
                    specification.optionList=response.data;
                    var specificationJSON = JSON.stringify(specification);
                    vue.specification=JSON.parse(specificationJSON);
                });
            },
            checkAll:function (event)
            {
                this.ids=[];
                if(event.target.checked)
                {
                    for(var i=0;i<this.specificationList.length;i++)
                    {
                        this.ids.push(this.specificationList[i].id);
                    }
                }
            },
            del:function ()
            {
                if(this.ids.length>0)
                {
                    axios.get("/specification/delete?ids="+this.ids).then(function (response)
                    {
                        if(response.data)
                        {
                            vue.current=((vue.current==vue.pages && vue.checked)?vue.current-1:vue.current);
                            if(vue.current<=0)
                            {
                                vue.current=1;
                            }
                            vue.findByPage(vue.current);
                        }
                        else
                        {
                            alert("删除失败！");
                        }
                    })
                }
                else
                {
                    alert("请选择要删除的记录");
                }
            }
        },
        created:function ()
        {
            this.findByPage(this.current);
        },
        updated:function ()
        {
            this.checked=(this.ids.length==this.specificationList.length)
        }
    })
};